package com.example.testapplication.fragment;

import android.util.Log;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.testapplication.R;
import com.example.testapplication.adapter.AddressAdapter;
import com.example.testapplication.adapter.OrderMsgAdapter;
import com.example.testapplication.adapter.ShoppingDataAdapter;
import com.example.testapplication.api.Api;
import com.example.testapplication.api.ApiConfig;
import com.example.testapplication.api.CallBack;
import com.example.testapplication.entity.AddressEntity;
import com.example.testapplication.entity.Order;
import com.example.testapplication.entity.ShoppingMsgEntity;
import com.example.testapplication.entity.response.AddressResponse;
import com.example.testapplication.entity.response.OrderResponse;
import com.example.testapplication.entity.response.ShoppingMsgResponse;
import com.google.gson.Gson;
import com.scwang.smart.refresh.footer.ClassicsFooter;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class OrderMsgFragment extends BaseFragment {

    private int state;
    private RecyclerView recyclerView;
    private RefreshLayout refreshLayout;
    private OrderMsgAdapter orderMsgAdapter;
    List<Order> datas = new ArrayList<>();
    private int page=1;
    private int pageSize=5;

    public OrderMsgFragment(){

    }

    public static OrderMsgFragment newInstance(int state){
        OrderMsgFragment fragment = new OrderMsgFragment();
        fragment.state = state;
        return fragment;
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_order_msg;
    }

    @Override
    protected void initView() {
        recyclerView = mRootView.findViewById(R.id.recyclerViewOrder);
        refreshLayout = mRootView.findViewById(R.id.refreshLayoutOrder);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getActivity()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getActivity()));
    }

    @Override
    protected void initData() {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(linearLayoutManager);
        orderMsgAdapter =new OrderMsgAdapter(getActivity());
        Log.v("orderjslgh","result");
        recyclerView.setAdapter(orderMsgAdapter);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                getOrderList(true);
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                page++;
                //refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                getOrderList(false);
            }
        });
        getOrderList(true);
    }

    private void getOrderList(boolean isRefresh){
        Log.v("orderjslgh","result");
        HashMap<String,Object> params = new HashMap<>();
        params.put("page",page);
        params.put("pageSize",pageSize);
        params.put("state",state);
        Api.config(ApiConfig.USER_ORDER,params).getRequest(getActivity(), new CallBack() {
            @Override
            public void onSuccess(String result) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if(isRefresh){
                            refreshLayout.finishRefresh(true);
                        }else{
                            refreshLayout.finishLoadMore(true);
                        }
                        Log.v("orderjslgh",result);
                        Gson gson = new Gson();
                        OrderResponse res = gson.fromJson(result, OrderResponse.class);
                        if(res.getCode() == 1){
                            Log.v("nima",result);
                            List<Order> lists = res.getData().getPageBean().getRows();
                            if(lists!=null){
                                if(isRefresh){
                                    datas = lists;
                                }else{
                                    datas.addAll(lists);
                                }
                                orderMsgAdapter.setListDatas(datas);
                                orderMsgAdapter.notifyDataSetChanged();
                            }else {
                                if(isRefresh){
                                    showToast("暂时无法加载");
                                }else{
                                    showToast("没有更多啦");
                                }
                            }
                        }else{
                            showToastAsync("失败");
                        }
                    }
                });

            }

            @Override
            public void onFailure(IOException e) {
                if(isRefresh){
                    refreshLayout.finishRefresh(true);
                }else{
                    refreshLayout.finishLoadMore(true);
                }
            }
        });
    }
}
